package com.zhangqiancheng.wcdmaconsole.db;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DbHelper extends SQLiteOpenHelper {

	public static final String DATABASE_NAME = "WCDMA_CONSOLE.db";
	public static final int DATABASE_VERSION = 1;

	public final static String WCDMA_TABLE = "wcdma";
	public final static String WATCH_TABLE = "watch";

	public static final String KEY_ID = "_id";

	public static final String WCDMA_COLUMN_IMSI = "imsi";
	public static final String WCDMA_COLUMN_IMEI = "imei";
	public static final String WCDMA_COLUMN_NOTE = "note";
	public static final String WCDMA_COLUMN_CREATEDTIME = "created_time";

	public static final String WATCH_COLUMN_IMSI = "imsi";
	public static final String WATCH_COLUMN_IMEI = "imei";
	public static final String WATCH_COLUMN_NOTE = "note";
	public static final String WATCH_COLUMN_CREATEDTIME = "created_time";

	public static final String KEY_ID_ASC = KEY_ID + " ASC";
	public static final String KEY_ID_DESC = KEY_ID + " DESC";
	public static final String CREATED_TIME_ASC = "created_time ASC";
	public static final String CREATED_TIME_DESC = "created_time DESC";

	private static final String WCDMA_CREATE = "CREATE TABLE IF NOT EXISTS " + WCDMA_TABLE + "(" +
			KEY_ID + " integer primary key autoincrement, " +
			WCDMA_COLUMN_IMSI + " varchar(50), " +
			WCDMA_COLUMN_IMEI + " varchar(50), " +
			WCDMA_COLUMN_NOTE + " varchar(100), " +
			WCDMA_COLUMN_CREATEDTIME + " timestamp default CURRENT_TIMESTAMP);";

	private static final String WATCH_CREATE = "CREATE TABLE IF NOT EXISTS " + WATCH_TABLE + "(" +
			KEY_ID + " integer primary key autoincrement, " +
			WATCH_COLUMN_IMSI + " varchar(50), " +
			WATCH_COLUMN_IMEI + " varchar(50), " +
			WATCH_COLUMN_NOTE + " varchar(100), " +
			WATCH_COLUMN_CREATEDTIME + " timestamp DEFAULT CURRENT_TIMESTAMP);";

	private static DbHelper instance;

	private DbHelper(Context context, String name, CursorFactory factory, int version) {

		super(context, name, factory, version);
	}

	public static DbHelper getInstance(Context context, String name, CursorFactory factory, int version) {

		if (instance == null)
			instance = new DbHelper(context, name, factory, version);

		return instance;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

		db.execSQL(WCDMA_CREATE);
		db.execSQL(WATCH_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

		db.execSQL("DROP TABLE IF EXISTS " + WCDMA_TABLE);
		db.execSQL("DROP TABLE IF EXISTS " + WATCH_TABLE);
		onCreate(db);
	}

	public static void insertDemo(Context context) {

		SQLiteDatabase db = DbHelper.getInstance(context, DATABASE_NAME, null, DATABASE_VERSION).getWritableDatabase();
		
		for (int i = 0; i < 100; i++)
		{
			ContentValues values = new ContentValues();
			values.put("imsi", (i+1) + "1111");
			values.put("imei", (i+1) + "1111");
			values.put("note", "note " + (i+1));
			db.insert(WCDMA_TABLE, null, values);
		}
	}
	
	public void deleteDemo(Context context) {
		
	}

}
